

<template>
  <div>
    <el-dialog v-bind="$attrs" :visible.sync="showDialog" v-on="$listeners" @open="onOpen" @close="onClose" title="注册">
      <el-row :gutter="15">
        <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
          <!-- <el-col :span="24">
            <el-form-item label="上传" prop="field101" required>
              <el-upload ref="field101" :file-list="field101fileList" :action="field101Action"
                :before-upload="field101BeforeUpload">
                <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
              </el-upload>
            </el-form-item>
          </el-col> -->
          <el-col :span='24'> 
            <!-- 用户头像 -->
            <el-form-item label="用户头像">
              <!-- 头像略缩图 -->
              <pan-thumb :image="formData.avatar"/> 
              <!-- <pan-thumb  :image="String(filePath)"> -->

              <!-- 文件上传按钮 -->
              <el-button type="primary" icon="el-icon-upload" @click="imagecropperShow=true">更换头像
              </el-button>

              <image-cropper
                v-show="imagecropperShow"
                :width="300"
                :height="300"
                :key="imagecropperKey"
                :url="'/uploadImgFile'"
                field="multipartFile"
                @close="closeImage"
                @crop-upload-success="cropSuccess"/>

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="userName">
              <el-input v-model="formData.userName" placeholder="请输入姓名" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-radio-group v-model="formData.gender" size="medium">
                <el-radio v-for="(item, index) in genderOptions" :key="index" :label="item.value"
                  :disabled="item.disabled">{{item.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年龄" prop="age">
              <el-input v-model="formData.age" placeholder="请输入年龄" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="formData.phone" placeholder="请输入联系电话" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码" prop="password">
              <el-input v-model="formData.password" placeholder="请输入密码" clearable show-password
                :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="过敏源" prop="allergy">
              <el-input v-model="formData.allergy" type="textarea" placeholder="请输入过敏源"
                :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="既往病史" prop="medicalHistory">
              <el-input v-model="formData.medicalHistory" type="textarea" placeholder="请输入既往病史"
                :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import ImageCropper from '../../components/ImageCropper'
import PanThumb from '../../components/PanThumb'

import {updateUser} from '../../api/users'
import {saveUser} from '../../api/users'
import {saveRoleUser} from '../../api/role'
export default {
  inheritAttrs: false,
  components: {
    ImageCropper,
    PanThumb,
  },
  props: {
    addOrUpdateVisible:{
      type:Boolean,
      default:false
    },
   
}
,
  data() {
    return {
      formData: {
        userName: undefined,
        gender: 1,
        age: undefined,
        phone: undefined,
        password: undefined,
        allergy: undefined,
        medicalHistory: undefined,
        userId:'',
        avatar:'https://pic1.zhimg.com/80/v2-6afa72220d29f045c15217aa6b275808_1440w.jpg',
      },
      roleUser:{
        roleId: 3,
        userId:''
      },
      showDialog:false,
      imagecropperShow: false,
      imagecropperKey: 0,
      image: '',
      rules: {
        userName: [{
          required: true,
          message: '请输入姓名',
          trigger: 'blur'
        }],
        gender: [{
          required: true,
          message: '性别不能为空',
          trigger: 'change'
        }],
        age: [{
          required: true,
          message: '请输入年龄',
          trigger: 'blur'
        }],
        phone: [{
          required: true,
          message: '请输入联系电话',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }],
        allergy: [{
          required: true,
          message: '请输入过敏源',
          trigger: 'blur'
        }],
        medicalHistory: [{
          required: true,
          message: '请输入既往病史',
          trigger: 'blur'
        }],
      },
      
      field101Action: 'https://jsonplaceholder.typicode.com/posts/',
      field101fileList: [],
      genderOptions: [{
        "label": "男",
        "value": 1
      }, {
        "label": "女",
        "value": 0
      }],
    }
  },
  computed: {},
  watch:{
      addOrUpdateVisible(oldVal,newVal){
      this.showDialog = this.addOrUpdateVisible
    }
      },
  created() {
   
  },
  mounted() {},
  methods: {
    onOpen() {},
    onClose() {
      this.$refs['elForm'].resetFields()
      this.$emit('changeShow','false')
    },
    close() {
      this.$emit('update:visible', false)
      this.$emit('changeShow','false')
    },
    //确认
    handelConfirm() {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        console.log(this.formData)
       this.addUser();
      })
    },
    //新增
    async addUser() {
      const { data:res } = await saveUser(this.formData)
      if (res.code == 200) {
        this.roleUser.userId = res.data.userId
        const res2 = await saveRoleUser(this.roleUser)
       console.log(res2)
        this.$notify.success({
          title: '操作成功',
          message: '用户注册成功',
        })
        //关闭
        this.$emit('changeShow','false')
        this.formData = {}
        this.$emit('getDepartment');
      //  this.$emit('getDeptAndCount');
      } else {
        return this.$message.error('用户注册失败：'+res.message)
      }
    },
    
    field101BeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 2
      if (!isRightSize) {
        this.$message.error('文件大小超过 2MB')
      }
      return isRightSize
    },
    //上传成功后的回调函数
    cropSuccess(data) {
      console.log(data)
 
      this.imagecropperShow = false;
      console.log(data.data.url);
      this.formData.avatar = data.data.url;
      //上传成功后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1
      //上传后删除之前的头像
      console.log('avatar↓');
   //   console.log(this.user.avatar);
      //this.deleteAvatar(oldImage);
    },
     //关闭上传组件
    closeImage() {
      this.imagecropperShow = false
      //上传失败后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1
    },

  },
  
}

</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}

</style>
